package org.snare.alumni.modules.admin.reservation.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.snare.alumni.modules.admin.card.dto.CardDTO;
import org.snare.alumni.modules.admin.reservation.mapper.ReservationMapper;
import org.snare.alumni.modules.admin.reservation.entity.ReservationEntity;
import org.snare.alumni.modules.admin.reservation.service.api.ReservationService;
import org.snare.spider.common.base.Limit;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;


@Service
@RequiredArgsConstructor
public class ReservationServiceImpl implements ReservationService {

    private final ReservationMapper reservationMapper;

    @Override
    public IPage<ReservationEntity> page(ReservationEntity params, Limit limit) {
        return new LambdaQueryChainWrapper<>(reservationMapper)
                .eq(StringUtils.isNotBlank(params.getUid()), ReservationEntity::getUid, params.getUid())
                .eq(StringUtils.isNotBlank(params.getPhone()), ReservationEntity::getPhone, params.getPhone())
                .eq(StringUtils.isNotBlank(params.getName()), ReservationEntity::getName, params.getName())
                .orderByDesc(ReservationEntity::getCreateTime)
                .page(limit.page());
    }

    @Override
    public void add(ReservationEntity params) {
        Long count = new LambdaQueryChainWrapper<>(reservationMapper)
                .eq(ReservationEntity::getPhone, params.getPhone())
                .eq(ReservationEntity::getName, params.getName()).count();
        if (count == 0) {
            params.setStatus(0);
            reservationMapper.insert(params);
        }
    }

    @Override
    public void delete(String id) {
        reservationMapper.deleteById(id);
    }

    @Override
    public void approve(Map<String, String> params) {
        if (StringUtils.isBlank(params.get("id")) || params.get("status") == null) {
            throw new RuntimeException("参数错误");
        }
        new LambdaUpdateChainWrapper<>(reservationMapper)
                .set(ReservationEntity::getStatus, params.get("status"))
                .eq(ReservationEntity::getId, params.get("id"))
                .update();
    }

    @Override
    public void cancel(Map<String, String> params) {
        if (StringUtils.isBlank(params.get("id")) || StringUtils.isBlank(params.get("opinion"))) {
            throw new RuntimeException("参数错误");
        }
        new LambdaUpdateChainWrapper<>(reservationMapper)
                .set(ReservationEntity::getStatus, "-1")
                .set(ReservationEntity::getOpinion, params.get("opinion"))
                .eq(ReservationEntity::getId, params.get("id"))
                .update();
    }


}
